<template>
  <div>
    <avue-crud
      :data="data"
      :option="optionInfo"
      :page.sync="page"
      :table-loading='tableLoading'
      @on-load="getList"
      @row-save="rowSave"
      @search-change='search'
      @search-reset='getList(page)'
      @row-del='rowDel'
      @row-update='rowUpdate'
    ></avue-crud>
  </div>
</template>
<script>
import { getownerinfo, addtoUser,deleteYz } from "@/api/user.js";
import { optionInfo } from "@/const/crud/user/userinfo.js";
export default {
  data() {
    return {
      optionInfo: optionInfo.apply(this),
      data: [],
      tableLoading:true,
      page: {
        total: 0,
        currentPage: 1,
        pageSize: 10,
      },
    };
  },
  created() {
    console.log(this)
  },
  methods: {
    getList(page,search={},dom) {
      this.tableLoading=true
      getownerinfo({
        page: page.currentPage,
        pageNum: page.pageSize,
        search
      }).then(res=>{
        res.data.items.forEach(item=>{
					item.house_numLD=JSON.parse(item.house_numLD)
				})
        this.page.total=res.data.total
        this.data=res.data.items
        this.tableLoading=false
        if(dom)dom()
      })
    },
    // 添加住户
    rowSave(row, dom) {
      row.house_numLD = JSON.stringify(row.house_numLD);
      delete row.$sex;
      delete row.$race;
      delete row.$house_numLD;
      delete row.$vaccination;
      addtoUser(row).then((res) => {
        this.$message({
          message: "添加成功",
          type: "success",
        });
        dom();
        this.getList(this.page);
      },err=>dom());
    },
    rowUpdate(row,_,dom){
      row.house_numLD = JSON.stringify(row.house_numLD);
      delete row.$sex;
      delete row.$race;
      delete row.$house_numLD;
      delete row.$vaccination;
      delete row.$index
      addtoUser(row).then((res) => {
        this.$message({
          message: "修改成功",
          type: "success",
        });
        dom();
        this.getList(this.page);
      },err=>dom());
    },
    rowDel(row){
      deleteYz({id:row.house_id}).then(res=>{
        this.$message({
          message: "删除成功",
          type: "success",
        })
        this.getList(this.page);
      })
    },
    // 搜索
    search(e,dom){
      this.getList(this.page,e,dom)
    }
  },
};
</script>